package com.liefox.serviceorder.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liefox.eduucenter.entity.UcenterMember;
import com.liefox.serviceedu.entity.frontVo.CourseWebVo;
import com.liefox.serviceorder.client.EduClient;
import com.liefox.serviceorder.client.UcenterClient;
import com.liefox.serviceorder.entity.Order;
import com.liefox.serviceorder.mapper.OrderMapper;
import com.liefox.serviceorder.service.OrderService;
import com.liefox.serviceorder.utils.OrderNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author liefox
 * @since 2021-07-22
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private EduClient eduClient;
    @Autowired
    private UcenterClient ucenterClient;


    @Override
    public String saveOrder(String courseId, String memberIdByJwtToken) {

        final CourseWebVo courseInfoOrder = eduClient.getCourseInfoOrder(courseId);

        final UcenterMember info = ucenterClient.getInfo(memberIdByJwtToken);

        final Order order = new Order();
        order.setOrderNo(OrderNoUtil.getOrderNo());
        order.setCourseId(courseId);
        order.setCourseTitle(courseInfoOrder.getTitle());
        order.setCourseCover(courseInfoOrder.getCover());
        order.setTeacherName(courseInfoOrder.getTeacherName());
        order.setTotalFee(courseInfoOrder.getPrice());
        order.setMemberId(memberIdByJwtToken);
        order.setEmail(info.getEmail());
        order.setNickname(info.getNickname());
        order.setStatus(0);
        order.setPayType(1);
        baseMapper.insert(order);

        return order.getOrderNo();
    }
}
